package com.zrrd.yunchmall.product.mq;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.zrrd.yunchmall.config.RabbitConfig;
import com.zrrd.yunchmall.product.entity.Product;
import com.zrrd.yunchmall.product.service.IProductService;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
@RabbitListener(queues = RabbitConfig.QUEUE_NAME)
public class MessageListener {
    @Autowired
    private IProductService productService;
    @RabbitHandler
    public void process(Map map) {

        //先从消息里取出商品id
        long productId = (long) map.get("productId");
        //使用商品id查询商品对象
        Product product = productService.getById(productId);
        //取出退货数量
        int returnCount = (int) map.get("productCount");
        //修改商品库存
        product.setStock(product.getStock() + returnCount);
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.set("stock", product.getStock());
        //只修改库存
        updateWrapper.eq("id", product.getId());
        //修改数据库
        productService.update(updateWrapper);

    }
}
